//
//  TestPwpViewController.swift
//  TestPwp
//
//  Created by Denny on 2018/11/28.
//  Copyright © 2018 Denny. All rights reserved.
//

import Cocoa

enum PWPWiFiTag {
    case None
    case Trusted
    case Unknown
}

enum PWPIssueStatus {
    case None
    case SecureWiFi
    case UnSecureWiFiAndVpnOn
    case UnSecureWiFiAndVpnOff
    case UnSecureTrustedWiFiAndVpnOn
    case UnSecureTrustedWiFiAndVpnOff
    case UnkonwnWiFiAndVpnOn
    case UnkonwnWiFiAndVpnOff
    case NoInternetConnectionIssues
    case NoInternetConnectionNoIssues
}

class TestPwpViewController: NSViewController,NSTableViewDelegate,NSTableViewDataSource {
    
    
    @IBOutlet weak var scroller1: NSScroller!
    @IBOutlet weak var scroller2: NSScroller!
    @IBOutlet weak var contentBackgroundView: NSView!
    @IBOutlet weak var scrollContentView: NSScrollView!
    @IBOutlet weak var scrollClipView: NSClipView!
    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var ssidNameTextField: NSTextField!
    @IBOutlet weak var ssidIssueTextField: NSTextField!
    @IBOutlet weak var issueTextField: NSTextField!
    
    @IBOutlet weak var addWiFiTextToListField: TMLinkLabel!
    
    @IBOutlet weak var arrowButton: NSButton!
    
    @IBOutlet weak var wifiTagButton: NSButton!
    
    @IBOutlet weak var issueTypeImageView: NSImageView!
    
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    
    var oriRectRemember = CGRect.zero;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.addWiFiTextToListField.clickClosure = { [weak self] (label) in
            
            self?.addWiFiToTrustedList();
            
        }
        // Do view setup here.
        
//        self.scroller1.wantsLayer = true;
//        self.scroller1.layer?.backgroundColor = NSColor.red.cgColor;
//
//        self.scroller2.wantsLayer = true;
//        self.scroller2.layer?.backgroundColor = NSColor.red.cgColor;
        
        self.contentBackgroundView.wantsLayer = true;
        self.contentBackgroundView.layer?.backgroundColor = NSColor.white.cgColor;
        
        self.tableView.headerView = nil;
        self.scrollContentView.drawsBackground = false;
        self.tableView.backgroundColor = NSColor.clear;
    }
    
    public func numberOfRows(in tableView: NSTableView) -> Int {
        return 4;
    }
    
    public func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        
        return 70;
    }
    
    public func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        
        if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "ImageCell"), owner: nil) as? NSTableCellView {
//            cell.isHidden = true;
            cell.wantsLayer = true;
            
            cell.layer?.backgroundColor = NSColor.clear.cgColor;
            
            cell.textField?.maximumNumberOfLines = 2;
            cell.textField?.usesSingleLineMode = false;
            cell.textField?.stringValue = "111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬111111你好阿斯顿发斯蒂芬";
            return cell;
        }
        
        return nil;
        
//        let textField = NSTextField.init(string: "11111");
//        textField.wantsLayer = true;
//        textField.backgroundColor = NSColor.clear;
//        textField.isBordered = false;
//        return textField;
    }
    
    @IBAction func clickWiFiTagAction(_ sender: Any) {
        
        let storyboard = NSStoryboard.init(name: NSStoryboard.Name.init("Main"), bundle: nil);
        let VC = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier.init("PWPTrustedWiFiListViewController")) as! PWPTrustedWiFiListViewController;
        self.presentAsSheet(VC)
    }
    
    @IBAction func clickArrowAction(_ sender: Any) {
        
        NSAnimationContext.runAnimationGroup { (context) in
            
            context.duration = 0.5;
            context.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeInEaseOut);
            
            if self.heightConstraint.constant == 140 {
                
                self.heightConstraint.animator().constant = 500;
                
            }else{
                self.heightConstraint.animator().constant = 140;
            }
        }
    }
    
    private func addWiFiToTrustedList() {
        
        let alert = NSAlert.init();
        alert.messageText = "Add To Trusted List";
        alert.alertStyle = .informational;
        alert.addButton(withTitle: "OK").setAccessibilityFocused(true);
        alert.addButton(withTitle: "Cancel");
        alert.beginSheetModal(for: self.view.window!) { (modalResponse) in
            
            
        }
    }
    
    func tableViewSelectionDidChange(_ notification: Notification) {
        let table = notification.object as! NSTableView
        print(table.selectedRow);
        
        let storyboard = NSStoryboard.init(name: NSStoryboard.Name.init("Main"), bundle: nil);
        let VC = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier.init("PWPSecurityIssuesViewController")) as! PWPSecurityIssuesViewController;
        self.presentAsSheet(VC)
    }
    
    public func updateTitle(_ title: String) {
        
        self.ssidNameTextField.stringValue = title;
        
    }
    
    public func updateWiFiTag(_ tag: PWPWiFiTag) {
        
        self.wifiTagButton.stringValue = "Trusted Wi-Fi";
    }
    
    public func updateIssueStatus(_ status: PWPIssueStatus) {
        
//        self.issueTypeImageView
//        self.ssidIssueTextField
    }
}
